
#include "sys_scene.h"

static scend_manage_t  scene_info;

int scene_info_add(scene_info_t *msg)
{
    scene_link_t * obj = NULL;

    if(!msg){
        return -1;
    }

	if(!scene_info.e_head){
		return -1;
	}

    obj = (scene_link_t *)psram_malloc(sizeof(scene_link_t));
    if(obj == NULL){
        l_d("obj err\n");
        return -1;
    }
    memset(obj, 0, sizeof(scene_link_t));
    slist_init(&(obj->e_tail));
    //**************************************************
	snprintf(obj->e_attr.homeid,sizeof(obj->e_attr.homeid),"%s",msg->homeid);
    snprintf(obj->e_attr.sceneName,sizeof(obj->e_attr.sceneName),"%s",msg->sceneName);
    snprintf(obj->e_attr.sceneId,sizeof(obj->e_attr.sceneId),"%s",msg->sceneId);
    snprintf(obj->e_attr.sceneIcon,sizeof(obj->e_attr.sceneIcon),"%s",msg->sceneIcon);
    //**************************************************
    slist_add_tail(scene_info.e_head,&(obj->e_tail));
    //**************************************************

    return 0;
}


int scene_info_del(char* sceneId)
{
	int ret = 0;

	slist_t* node;
	scene_link_t * obj = NULL;

	if(!scene_info.e_head){
		return -1;
	}

	if(slist_isempty(scene_info.e_head)){
		l_d("list empty.\n");
		return -1;
	}

	slist_for_each(node,(scene_info.e_head))
	{
		obj = slist_entry(node, scene_link_t, e_tail);
		if(obj)
        {
            if(!strcmp(obj->e_attr.sceneId,sceneId))
            {
                ret = 1;
                break;
            }
		}
	}

	if(ret){
		slist_remove(scene_info.e_head, &(obj->e_tail));
        psram_free(obj);
        obj = NULL;
	}

	return 0;
}

scene_info_t* scene_info_get_byindex(int index)
{
	int ret = 0;
	int scene_num = 0;
	slist_t* node;
	scene_link_t * obj  = NULL;
	scene_info_t*  info = NULL;

	if(!scene_info.e_head){
		return NULL;
	}

	if(slist_isempty(scene_info.e_head)){
		l_d("list empty.\n");
		return NULL;
	}

	slist_for_each(node,(scene_info.e_head))
	{
		obj = slist_entry(node, scene_link_t, e_tail);
		if(obj)
        {
            if(scene_num == index)
            {
				info = &(obj->e_attr);
                ret = 1;
                break;
            }
		}
		scene_num++;
	}

	if(ret){
		return info;
	}else{
		return NULL;
	}
}

int scene_info_is_add(char* sceneid)
{
    int ret = 0;
	slist_t* node;
    scene_link_t * obj = NULL;

	if(!scene_info.e_head){
		return 0;
	}

	if(slist_isempty(scene_info.e_head)){
		l_d("list empty.\n");
		return 0;
	}

	slist_for_each(node,(scene_info.e_head))
	{
		obj = slist_entry(node,scene_link_t,e_tail);
		if(obj)
        {		
            if(!strcmp(obj->e_attr.sceneId,sceneid))
            {
                ret = 1;
                break;
            }
		}
	}

	return ret;
}

int scene_info_get_num(void)
{
	int scene_len = 0;
	slist_t* node;
    scene_link_t * obj = NULL;

	if(!scene_info.e_head){
		return 0;
	}

	if(slist_isempty(scene_info.e_head)){
		l_d("list empty.\n");
		return 0;
	}

	slist_for_each(node,(scene_info.e_head))
	{
		obj = slist_entry(node,scene_link_t,e_tail);
		if(obj)
        {		
			scene_len++;
		}
	}

	return scene_len;
}

int scene_info_del_all(void)
{
	slist_t* node;
    scene_link_t * obj = NULL;

	if(!scene_info.e_head){
		return -1;
	}

	if(slist_isempty(scene_info.e_head)){
		l_d("list empty.\n");
		return -1;
	}

	slist_for_each(node,(scene_info.e_head))
	{
		obj = slist_entry(node,scene_link_t,e_tail);
		if(obj)
        {		
            slist_remove(scene_info.e_head, &(obj->e_tail));
            psram_free(obj);
            obj = NULL;
		}
	}

	return 0;
}

int scene_info_init(void)
{
    scene_info.e_head = (slist_t *)psram_malloc(sizeof(slist_t));
	if (scene_info.e_head == NULL) {
		l_d("e_head null\n");
		return -1;
	}

    slist_init(scene_info.e_head);

    return 0;
}

int scene_info_destroy(void)
{
	if(!scene_info.e_head){
		return -1;
	}

    scene_info_del_all();

    psram_free(scene_info.e_head);
    scene_info.e_head = NULL;

    return 0;
}